<?php
class AccessoryAction extends CommonAction{
	
	 public function _before_index()
	{
		$_REQUEST ['_order']='storeplace';
	} 

    public function read(){
    	$id=$_REQUEST['id'];
    	$model=M('Accessory');
    	$Accessory=$model->where('id='.$id)->find();
    	$this->assign('vo',$Accessory);
    	$this->display();
    }
	
	//辅料入库
	public function ainput(){
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$_SESSION['id']=$id;
		$vo = $model->getById ( $id );
		$this->assign ( 'vo', $vo );
		$this->display ();
	}
	public function insertinput(){
		$model=M('AccessoryStock');
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}				
		$accessory=M('Accessory');
		$id=$_SESSION['id'];
		$oldweight=$accessory->where('id='.$id)->getField('weight');
		$instock=$_REQUEST['stock'];
		$data['weight']=$oldweight+$instock;
		$accessory->where('id='.$id)->save($data);
		$data1['accid']=$id;
		$data1['accname']=$_REQUEST['accname'];
		$data1['stock']=$_REQUEST['stock'];
		$data1['type']=$_REQUEST['type'];
		$data1['note']=$_REQUEST['note'];
		if($_REQUEST['time']==null){
			$data1['time']=date('Ymd');
		}else{
			$data1['time']=$_REQUEST['time'];
		}
		$result=$model->add($data1);
		if(false !== $result){
			$this->ajaxReturn(1,'辅料入库成功！',200);
		}else{
			$this->ajaxReturn(1,'辅料入库失败！',300);
		}
	}
	/* //入库查询
	public function checkinput(){		
		$model=M('AccessoryInput');
		$accid=$_REQUEST['id'];
		import ( '@.Org.Page' );
		$count = $model->where('accid='.$accid)->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where('accid='.$accid)->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		$this->assign('list',$list);
		$this->assign ( 'id', $accid);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	} */
	//辅料出库
	public function aoutput(){
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$_SESSION['id']=$id;
		$vo = $model->getById ( $id );
		$this->assign ( 'vo', $vo );
		$this->display ();
	}
	public function insertoutput(){
		$model=M('AccessoryStock');
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}		
		$accessory=M('Accessory');
		$id=$_SESSION['id'];
		$oldweight=$accessory->where('id='.$id)->getField('weight');
		$outstock=$_REQUEST['stock'];
		if($outstock>$oldweight){
			$this->ajaxReturn(1,'辅料库存不足，请重新输入！',300);
		}
		$data['weight']=$oldweight-$outstock;
		$accessory->where('id='.$id)->save($data);
		$data1['accid']=$id;
		$data1['accname']=$_REQUEST['accname'];
		$data1['stock']=$outstock;
		$data1['type']=$_REQUEST['type'];
		$data1['note']=$_REQUEST['note'];
		dump($_REQUEST);
		if($_REQUEST['time']==null){
			$data1['time']=date('Ymd');
		}else{
			$data1['time']=$_REQUEST['time'];
		}
		$result=$model->add($data1);
		if(false !== $result){
			$this->ajaxReturn(1,'辅料出库成功！',200);
		}else{
			$this->ajaxReturn(1,'辅料出库失败！',300);
		}
	}
	/* //出库查询
	public function checkoutput(){
		$model=M('AccessoryOutput');
		$accid=$_REQUEST['id'];
		import ( '@.Org.Page' );
		$count = $model->where('accid='.$accid)->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where('accid='.$accid)->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		$this->assign('list',$list);
		$this->assign ( 'id', $accid);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	} */
	//查询
	public function check(){
		$model=M('AccessoryStock');
		$accid=$_REQUEST['id'];
		import ( '@.Org.Page' );
		$count = $model->where('accid='.$accid)->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where('accid='.$accid)->page ( $pageNum . "," . $page->listRows )->order('time desc')->select ();
		$this->assign('list',$list);
		$this->assign ( 'id', $accid);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	//导出功能
	public function export(){
	
		$model=D('AccessoryStock');
		$accname=$_REQUEST['accname'];
		if(!empty($accname)){
			$map['accname']=array('like',"%" . $accname . "%");
		}
	    $accessorystock=$model->where($map)->select();
	    for($i=0;$i<count($accessorystock);$i++){
	    	if($accessorystock[$i]['type']=='1'){
	    		$accessorystock[$i]['type']="入库";
	    	}else{
	    		$accessorystock[$i]['type']="出库";
	    	}
	    }
		$cname=date('Y-m-d');
		 
		$map = array('辅料名称','操作类型','出入库数量','出入库时间');
		$field=array('accname','type','stock','time');
		 
		$this->phpexcel($accessorystock, $cname, $map,$field);
		 
	}
}

?>
